<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 15/11/16
 * Time: 下午6:09
 */
$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();

$brands = array(
    '奥迪',
    '阿斯顿马丁',
    '奔驰',
    '别克',
    '比亚迪',
    '宝骏',
    '宝马',
    '北京',
    '本田',
    '保时捷',
    '宝龙',
    '宾利',
    '奔腾',
    '北汽',
    '标致',
    '巴博斯',
    '奔驰',
    '长安',
    '长城',
    '长安商用',
    '昌河',
    '传祺',
    '大通',
    '东风',
    '东风小康',
    '帝豪',
    'DS',
    '东南',
    '大发',
    '大众',
    '道奇',
    '大迪',
    '东风风度',
    '菲亚特',
    '富奇',
    '福迪',
    '丰田',
    '福特',
    '风神',
    '风行',
    '法拉利',
    '福田',
    '观致',
    '华北',
    '恒天',
    '红旗',
    '黄海',
    '哈弗',
    '幻速',
    '华颂',
    '海马',
    '华普',
    '汇众',
    '华泰',
    '华阳',
    '海格',
    '哈飞',
    '佳星',
    '解放',
    '江淮',
    '江南',
    '江铃',
    '九龙',
    '吉奥',
    '吉利',
    '金杯',
    'Jeep',
    '捷豹',
    '克莱斯勒',
    '凯翼',
    '卡威',
    '开瑞',
    '凯迪拉克',
    '力帆',
    '劳斯莱斯',
    '路特斯',
    '路虎',
    '铃木',
    '猎豹',
    '林肯',
    '雷诺',
    '兰博基尼',
    '陆风',
    '理念',
    '雷克萨斯',
    '莲花',
    '迈凯伦',
    '马自达',
    'MG',
    'MINI',
    '美亚',
    '玛莎拉蒂',
    '纳智捷',
    '欧朗',
    '欧宝',
    '讴歌',
    '启辰',
    '全球鹰',
    '启腾',
    '庆铃',
    '起亚',
    '奇瑞',
    '日产',
    '瑞麒',
    '荣威',
    '斯柯达',
    '三菱',
    '思铭',
    '双环',
    '赛宝',
    '绅宝',
    '双龙',
    '陕汽通家',
    '斯巴鲁',
    'Smart',
    '天马',
    '特斯拉',
    '腾势',
    '通用',
    '通田',
    '五十铃',
    '威兹曼',
    '威旺',
    '万丰',
    '五菱',
    '威麟',
    '沃尔沃',
    '雪铁龙',
    '西雅特',
    '新雅途',
    '现代',
    '雪佛兰',
    '夏利',
    '新凯',
    '云雀',
    '英菲尼迪',
    '英伦',
    '一汽',
    '永源',
    '野马',
    '扬子',
    '英致',
    '依维柯',
    '中顺',
    '中兴',
    '众泰',
    '中华',
    '中欧');

foreach($brands as $_brand){
    $series = querySeries($_brand);
    if(!is_null($series)){
        if($series->code == 'success'){
            $series = $series->data;
            foreach($series as $_series){
//                echo $_series->brandName . "\t" . $_series->factory . "\t" . $_series->seriesName . "\n";
                $models = queryModel($_series->brandName,$_series->seriesName);
                if(!is_null($models)){
                    if($models->code == 'success'){
                        $models = $models->data;
                        foreach($models as $_model){
                            echo $_series->brandName . "\t" . $_series->factory . "\t" . $_series->seriesName . "\t";
                            echo $_model->carModelId . "\t" . $_model->modelName . "\t" . $_model->listingYear . "\n";
                        }
                    }else{
                        echo $_series->brandName . ' ' .$_series->seriesName . " get models error\n";
                    }
                }else{
                    echo $_series->brandName . ' ' .$_series->seriesName . " get models error\n";
                }
                sleep(1);
            }
        }else{
            echo $_brand . " get series error\n";
        }
    }else{
        echo $_brand . " get series error\n";
    }
    sleep(1);
}

function queryModel($brand,$series){
    $url = 'http://auto.jd.com/queryModel?brand=' . urlencode(urlencode($brand)) . '&series=' . urlencode(urlencode($series));
    $response = Httpful::get($url)
        ->timeoutIn(5)
        ->sendsForm()->send();
    return $response->body;
}

function querySeries($brand){
    $url = 'http://auto.jd.com/querySeries?brand=' . urlencode(urlencode($brand));
    $response = Httpful::get($url)
        ->timeoutIn(5)
        ->sendsForm()->send();
    return $response->body;
}

